﻿Imports Ecodex.RestAPI
Imports System.Text
Imports System.ServiceModel
Imports System.Configuration


Public Class frmGetCertificateKey

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim servicio = ConfigurationManager.AppSettings.GetValues("urlServicio")
            Dim url As Uri = New Uri(servicio(0))
            Dim jsonApi As RestApi = New RestApi(url)
            Dim tokenService = jsonApi.GetToken(txtRFC.Text, Utilerias.Integrator_ID)
            Dim certKey As Models.CertificadoClave = jsonApi.GetClaveCertificado(tokenService)
            txtCertificateKey.Text = certKey.clave.ToString
        Catch appFault As FaultException(Of Exceptions.FallaAplicacion)
            Using errorForm = New frmError(TranslateFault.ToAppError(appFault.Detail))
                errorForm.ShowDialog()

            End Using
        Catch validationFault As FaultException(Of Exceptions.FallaValidacion)
            Using errorForm = New frmError(TranslateFault.ToValidationError(validationFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch sessionFault As FaultException(Of Exceptions.FallaSesion)
            Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch ex As Exception
            ShowUnknownError(ex.Message)
        End Try
    End Sub

    Public Sub ShowUnknownError(ByVal message As String)
        MessageBox.Show([String].Format("Error: {0}", message), "Ecodex Cancela Documento")
    End Sub
End Class